package com.guoguo.controller;


import com.guoguo.utils.FileUtils;


import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.service.UploadService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/8/22 0022.
 */
@Controller
@RequestMapping("/view")
public class UploadController extends BaseController {

    @Resource
    private UploadService uploadService;

    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public String upload() {
        System.out.println("test");

        return "/uploadFile";
    }


    /**
     * 批量上传
     *
     * @param request
     * @param fileName
     * @param albId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadList", method = RequestMethod.POST)
    public JSONObject uploadFiles(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile fileName, @RequestParam String albId, @RequestParam int isZip) {
        //文件上传到本地服务器
        List<String> list = FileUtils.springFileUpload(request, isZip);
        //文件上传到oss
        List<String> ossFile = uploadService.uploadFile(list);

        Map<String, String> imageIds = null;

        JSONObject jsonObject = new JSONObject();
        if (ossFile.size() > 0) {
            //保存到数据库中
            imageIds = uploadService.uploadFiles(albId, ossFile);
        }
        jsonObject.put("ids", imageIds);
        return returnResult(200, jsonObject, "");
    }


}
